using System.Windows.Forms;
using TeachingManagementSystem.BLL;
using TeachingManagementSystem.Models;

namespace TeachingManagementSystem.Forms.ClassroomManagement
{
    public partial class ClassroomEditForm : Form
    {
        private ClassroomBLL _classroomBLL;
        private Classroom _classroom;
        private bool _isEditMode;

        public ClassroomEditForm(Classroom classroom)
        {
            InitializeComponent();
            _classroomBLL = new ClassroomBLL();
            _classroom = classroom;
            _isEditMode = classroom != null;
            InitializeForm();
            // 设置为适当的窗口大小
            this.WindowState = FormWindowState.Normal;
            this.Size = new System.Drawing.Size(650, 500);
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void InitializeForm()
        {
            // 初始化教室类型下拉框
            comboBoxType.DataSource = Enum.GetValues(typeof(ClassroomType));

            if (_isEditMode)
            {
                // 编辑模式，填充表单
                this.Text = "编辑教室";
                txtClassroomID.Text = _classroom.ClassroomID.ToString();
                txtClassroomNumber.Text = _classroom.ClassroomNumber;
                comboBoxType.SelectedItem = _classroom.Type;
                txtCapacity.Text = _classroom.Capacity.ToString();
                txtLocation.Text = _classroom.Building;
                txtEquipment.Text = _classroom.Equipment;
                btnSave.Text = "更新";
            }
            else
            {
                // 添加模式
                this.Text = "添加教室";
                txtClassroomID.Text = "自动生成";
                txtClassroomID.ReadOnly = true;
            }
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.txtClassroomID = new System.Windows.Forms.TextBox();
            this.txtClassroomNumber = new System.Windows.Forms.TextBox();
            this.comboBoxType = new System.Windows.Forms.ComboBox();
            this.txtCapacity = new System.Windows.Forms.TextBox();
            this.txtLocation = new System.Windows.Forms.TextBox();
            this.txtEquipment = new System.Windows.Forms.TextBox();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(60, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(52, 15);
            this.label1.TabIndex = 0;
            this.label1.Text = "教室ID";
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(40, 55);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(52, 15);
            this.label2.TabIndex = 1;
            this.label2.Text = "教室编号";
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(40, 90);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(52, 15);
            this.label3.TabIndex = 2;
            this.label3.Text = "教室类型";
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(40, 125);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(52, 15);
            this.label4.TabIndex = 3;
            this.label4.Text = "容量";
            // 
            // label5
            // 
            this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(40, 160);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(52, 15);
            this.label5.TabIndex = 4;
            this.label5.Text = "位置";
            // 
            // label6
            // 
            this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(40, 195);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(52, 15);
            this.label6.TabIndex = 5;
            this.label6.Text = "设备情况";
            // 
            // txtClassroomID
            // 
            this.txtClassroomID.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtClassroomID.Location = new System.Drawing.Point(150, 25);
            this.txtClassroomID.Name = "txtClassroomID";
            this.txtClassroomID.Size = new System.Drawing.Size(250, 25);
            this.txtClassroomID.TabIndex = 6;
            this.txtClassroomID.ReadOnly = true;
            // 
            // txtClassroomNumber
            // 
            this.txtClassroomNumber.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtClassroomNumber.Location = new System.Drawing.Point(120, 50);
            this.txtClassroomNumber.Name = "txtClassroomNumber";
            this.txtClassroomNumber.Size = new System.Drawing.Size(200, 25);
            this.txtClassroomNumber.TabIndex = 7;
            // 
            // comboBoxType
            // 
            this.comboBoxType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBoxType.FormattingEnabled = true;
            this.comboBoxType.Location = new System.Drawing.Point(120, 85);
            this.comboBoxType.Name = "comboBoxType";
            this.comboBoxType.Size = new System.Drawing.Size(200, 23);
            this.comboBoxType.TabIndex = 8;
            // 
            // txtCapacity
            // 
            this.txtCapacity.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtCapacity.Location = new System.Drawing.Point(120, 120);
            this.txtCapacity.Name = "txtCapacity";
            this.txtCapacity.Size = new System.Drawing.Size(200, 25);
            this.txtCapacity.TabIndex = 9;
            // 
            // txtLocation
            // 
            this.txtLocation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtLocation.Location = new System.Drawing.Point(120, 155);
            this.txtLocation.Name = "txtLocation";
            this.txtLocation.Size = new System.Drawing.Size(200, 25);
            this.txtLocation.TabIndex = 10;
            // 
            // txtEquipment
            // 
            this.txtEquipment.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtEquipment.Location = new System.Drawing.Point(150, 195);
            this.txtEquipment.Multiline = true;
            this.txtEquipment.Name = "txtEquipment";
            this.txtEquipment.Size = new System.Drawing.Size(300, 100);
            this.txtEquipment.TabIndex = 11;
            // 
            // btnSave
            // 
            this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnSave.Location = new System.Drawing.Point(150, 320);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(100, 35);
            this.btnSave.TabIndex = 12;
            this.btnSave.Text = "保存";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnCancel.Location = new System.Drawing.Point(280, 320);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(100, 35);
            this.btnCancel.TabIndex = 13;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // ClassroomEditForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = true;
            this.ClientSize = new System.Drawing.Size(650, 500);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.txtEquipment);
            this.Controls.Add(this.txtLocation);
            this.Controls.Add(this.txtCapacity);
            this.Controls.Add(this.comboBoxType);
            this.Controls.Add(this.txtClassroomNumber);
            this.Controls.Add(this.txtClassroomID);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "ClassroomEditForm";
            this.Text = "教室信息";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void btnSave_Click(object? sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrEmpty(txtClassroomNumber.Text.Trim()))
            {
                MessageBox.Show("教室编号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtClassroomNumber.Focus();
                return;
            }

            if (!int.TryParse(txtCapacity.Text.Trim(), out int capacity) || capacity <= 0)
            {
                MessageBox.Show("请输入有效的容量", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCapacity.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtLocation.Text.Trim()))
            {
                MessageBox.Show("位置不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtLocation.Focus();
                return;
            }

            try
            {
                if (_isEditMode)
                {
                    // 更新教室
                    _classroom.ClassroomNumber = txtClassroomNumber.Text.Trim();
                    _classroom.Type = comboBoxType.SelectedItem is ClassroomType classroomType ? classroomType : ClassroomType.Regular;
                    _classroom.Capacity = capacity;
                    _classroom.Building = txtLocation.Text.Trim();
                    _classroom.Equipment = txtEquipment.Text.Trim();

                    _classroomBLL.UpdateClassroom(_classroom);
                    MessageBox.Show("教室信息已更新", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    // 添加新教室
                    Classroom newClassroom = new Classroom
                    {
                        ClassroomNumber = txtClassroomNumber.Text.Trim(),
                        Type = comboBoxType.SelectedItem != null ? (ClassroomType)comboBoxType.SelectedItem : ClassroomType.Regular,
                        Capacity = capacity,
                        Building = txtLocation.Text.Trim(),
                        Equipment = txtEquipment.Text.Trim()
                    };

                    _classroomBLL.AddClassroom(newClassroom);
                    MessageBox.Show("教室添加成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object? sender, EventArgs e)
        {
            this.Close();
        }

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox txtClassroomID;
        private System.Windows.Forms.TextBox txtClassroomNumber;
        private System.Windows.Forms.ComboBox comboBoxType;
        private System.Windows.Forms.TextBox txtCapacity;
        private System.Windows.Forms.TextBox txtLocation;
        private System.Windows.Forms.TextBox txtEquipment;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnCancel;
    }
}